package com.yjht.online.arg.reslove;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.yjht.online.arg.ArgParam;
import com.yjht.online.arg.excption.ArgError;

import java.util.*;


public abstract class ArgResloveSplite implements ArgResolve {

    public ArgParam reslove(String[] args) throws ArgError {
        HashMap<String, List<String>> paramMap = new HashMap();
        String currentKey = "";
        for (String arg : args) {

            if (arg.startsWith("-")) {
                paramMap.put(arg, new ArrayList<String>());
                currentKey = arg;
                continue;
            }
            if (StrUtil.isBlank(currentKey)) {
                throw new ArgError("参数错误");
            }
            List<String> values = paramMap.get(currentKey);
            values.add(arg);
        }

        if (CollectionUtil.isEmpty(paramMap)) {
            return null;
        }

        ArgParam argParam = new ArgParam();
        paramMap.forEach((key, val) -> {
            reslove(argParam, key, val);
        });
        return argParam;
    }

    /**
     * 解析参数
     *
     * @param argName
     * @param argValue
     * @return
     */
    protected abstract void reslove(ArgParam argParam, String argName, List<String> argValue);
}
